package top.xiaoshuaila.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.xiaoshuaila.pojo.Admin;
import top.xiaoshuaila.pojo.AdminLoginParam;
import top.xiaoshuaila.pojo.RespBean;
import top.xiaoshuaila.service.IAdminService;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
public class LoginController {

    @Autowired
    private IAdminService adminService;
    @RequestMapping("/Login")
    public RespBean login(AdminLoginParam adminLoginParam,
                          HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),
                adminLoginParam.getPassword(),request);
    }
    @RequestMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if(null==principal)
        {
            return null;
        }
        String username=principal.getName();
        Admin admin=adminService.getAdminByUserName(username);
        admin.setPassword(null);
        return admin;

    }
    @RequestMapping("/Logout")
    public RespBean logout(){
        System.out.println("Logout");
        return RespBean.success("退出成功!");
    }
}